#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <stdio.h>
#include <stdint.h>
#include <wget.h>

int main()
{
  char *method = "POST",
       *uri = "192.168.1.9:3333/";//,
  //char *post_data = "{test:\"ss\"}";
  char *post_data = "<dev test=\"ss\"/>";
  size_t data_len = strlen(post_data);
  int ret = false;
  void *res = NULL;

  wget_http_connection *conn = NULL;
  wget_http_request *req;
  wget_http_response *resp;
  wget_iri *iri;

  printf("[DEBUG] [L%d f-%s] method:%s url:%s\n", __LINE__, __func__, method, uri);
  iri = wget_iri_parse(uri, NULL);
  req = wget_http_create_request(iri, method);
  wget_http_add_header(req, "Connection", "keepalive");
  if (post_data) {
   // printf("[DEBUG] [L%d f-%s] body:%s\n", __LINE__, __func__, post_data);
    //wget_http_request_set_body(req, "application/x-www-form-urlencoded",
    wget_http_request_set_body(req, "application/xml",
        wget_memdup(post_data, data_len), data_len);
  }
  wget_http_open(&conn, iri);
  if (conn) {
    if (!wget_http_send_request(conn, req)) {
      resp = wget_http_get_response(conn);
      if (resp && (resp->body != NULL)) {
        printf("reason:%s code:%d \n", resp->reason, resp->code);
        res = calloc(1, resp->body->length+2);
        memcpy(res, resp->body->data, resp->body->length);
        wget_http_free_response(&resp);
      }
    }
    while (1) {
      sleep(1);
      if (!wget_http_send_request(conn, req)) {
        resp = wget_http_get_response(conn);
        if (resp && (resp->body != NULL)) {
          printf("reason:%s code:%d \n", resp->reason, resp->code);
          res = calloc(1, resp->body->length+2);
          memcpy(res, resp->body->data, resp->body->length);
          wget_http_free_response(&resp);
        }
      }
    }
  }
  wget_http_free_request(&req);
  wget_iri_free(&iri);
  wget_http_close(&conn);
//  wget_global_deinit();
  printf("[DEBUG]L%d f-%s res:%s\n", __LINE__, __func__, (char *)res);

  return ret;
}
